<?php
/**
 * Created by PhpStorm.
 * User: 2016-08-23
 * Date: 2017/11/6
 * Time: 10:46
 */
$api_service_path = __DIR__ . "/../../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();
DB::connection()->disableQueryLog(); 
$beijin=1;//北京的城市id
$laiguangying=7;//来广营的门店id
$lgy_mdb=MaintenanceShops::find($laiguangying);
/**
 * 用于金蝶推送账套的脚本每月执行一次
 */

// $mdb=MaintenanceShops::where("status",1)->get();//循环各个门店的入库出库各种记录
// $_date=array(date('Y-m-01', strtotime('-1 month')),date('Y-m-t', strtotime('-1 month')));

// $mdb=MaintenanceShops::where("status",1)->where("id","!=",5)->get();
// $mdb=MaintenanceShops::whereIn("id",array(3))->get();
// $_date=array("2018-07-02","2018-07-31");
// $mdb=MaintenanceShops::whereIn("id",array(1))->get();//循环各个门店的入库出库各种记录
// $_date=array("2018-02-11","2018-02-11");
$mdb=MaintenanceShops::where("status",1)->where("id","!=",5)->get();//循环各个门店的入库出库各种记录
$c_date=date('Y-m-d',time()-86400);//前一天
$_date=array($c_date,$c_date);
// $_date=array("2018-10-24","2018-10-24");

Log::useFiles(storage_path().'/logs/kingdee_push.log');

$temp_shop_array=array();
$temp_mdb=MaintenanceShops::where("status",1)->get();
foreach ($temp_mdb as $_m) {
    $temp_shop_array[$_m->id]=array("xj"=>array(),"bx"=>array());
} 

        
$cur_date=floor((strtotime($_date[1])-strtotime($_date[0]))/86400);
for($i=0;$i<=$cur_date;$i++){   
    $date=date('Y-m-d',strtotime($_date[0])+$i*86400);       
    foreach ($mdb as $_mdb) {//
        if(!$_mdb->kingdee_groupid){
            Log::error("门店 ".$_mdb->id."金蝶科目部门异常");
            continue;
        }
        $tkvs=KingdeeVoucherStatus::where("date",$date)->where("type","liushui_inout")->where("shop_id",$_mdb->id)->where("status",1)->first();
        if($tkvs){//如果该门店 该天已经推送过凭证了
            continue;
        }

        $data=array('mainVoucher' =>
            array(
                    
            )
        );
        $entty_array=array();//组织所有的最基层 结构
        $inputjie = array(
            "FAccountID"=>"1243",
            "FExplanation"=>"",
            "FCurrencyID"=>1,
            "FExchangeRate"=>1,
            "FDC"=>1,
            "FAmountFor"=>100,
            "FAmount"=>100,
            "data"=>array(
                "009"=>"114",//商品分类
                "012"=>"001",//直营店
            ),
        );
        $inputdai = array(
            "FAccountID"=>"1243",
            "FExplanation"=>"脚本测试1",
            "FCurrencyID"=>1,
            "FExchangeRate"=>1,
            "FDC"=>0,
            "FAmountFor"=>100,
            "FAmount"=>100,
            "data"=>array(
                "009"=>"114",//商品分类
                "012"=>"001",//直营店
            ),
        );

        $begin_date=date("Y-m-d H:i:s",strtotime($date." 00:00:00")-Config::get("config.report_time_diff"));
        $end_date=date("Y-m-d H:i:s",strtotime($date." 23:59:59")-Config::get("config.report_time_diff"));
        
      
        /**
         * 流水的相关
         */
        $xj_msk=MaintenanceShopKingdee::where("shop_id",$_mdb->id)->where("code","1001")->first();//本门店的现金账套
        if(is_null($xj_msk)){
            Log::error("门店 ".$_mdb->id."现金科目异常");
            continue 1;//有错误数据
        }
        $ower_msk=MaintenanceShopKingdee::where("shop_id",$_mdb->id)->where("code","1133.01")->first();//内部往来科目异常
        if(is_null($ower_msk)){
            Log::error("门店 ".$_mdb->id."内部往来科目异常");
            continue 1;//有错误数据 跳出当天的 查询
        }
        $sk_msk=MaintenanceShopKingdee::where("shop_id",$_mdb->id)->where("code","1002")->first();//银行存款科目异常
        if(is_null($sk_msk)){
            Log::error("门店 ".$_mdb->id."银行存款科目异常");
            continue 1;//有错误数据 跳出当天的 查询
        }
        //现金收入开始
        //订单现金收入
        $xj_array=$ys_array=$fianly=array();
        $o_amount=DB::select("SELECT sum(opr.amount) as amount FROM
            order_pay_record opr
            LEFT JOIN orders o ON o.order_id = opr.order_id
            WHERE  o.`status` IN (1018, 1017, 1217, 1317, 1417)
            AND o.pay_time BETWEEN ? AND ? AND o.shop_id = ?
            AND opr.pay_type IN (1)
            AND opr.`status` = 1", array($begin_date, $end_date,$_mdb->id));
        //洗车现金收入
        $c_amount=DB::select("SELECT  sum(cpr.amount) as amount FROM
            car_wash_order_pay_record cpr
            LEFT JOIN car_wash_order co ON co.order_id = cpr.order_id
            WHERE  co.`status` IN (1018, 1017, 1217, 1317, 1417)
            AND co.pay_time BETWEEN ? AND ? AND co.shop_id = ?
            AND cpr.pay_type IN (1)
            AND cpr.`status` = 1", array($begin_date, $end_date,$_mdb->id));
        //套餐卡现金收入
        $s_amount=DB::select("SELECT sum(scpr.amount) as amount FROM
            service_card_order_pay_record scpr
            LEFT JOIN service_card_order sco ON sco.order_id = scpr.order_id
            WHERE sco.`status` IN (1018, 1017, 1217, 1317, 1417)
            AND sco.pay_time BETWEEN ? AND ? AND sco.shop_id = ?
            AND scpr.pay_type IN (1)
            AND scpr.`status` = 1", array($begin_date, $end_date,$_mdb->id));
        $xj_amount= $o_amount[0]->amount+$c_amount[0]->amount+$s_amount[0]->amount;

        assemLiushuiXJ($xj_array,$xj_amount,$inputjie,$inputdai,$xj_msk->acount,$date,$_mdb->kingdee_shopnum);	
        unset($o_amount);
        unset($c_amount);    
        unset($s_amount);    
        //现金收入完毕
        //支票收入和
        //订单支票
        $ozp_amount=DB::select("SELECT sum(opr.amount) as amount ,u.real_name FROM
            order_pay_record opr
            LEFT JOIN orders o ON o.order_id = opr.order_id	
			left join users u on u.id=o.pay_user_id
            WHERE  o.`status` IN (1018, 1017, 1217, 1317, 1417)
            AND o.pay_time BETWEEN ? AND ? AND o.shop_id = ?
            AND opr.pay_type =11
            AND opr.`status` = 1 group by o.order_id", array($begin_date, $end_date,$_mdb->id));  
        foreach ($ozp_amount as $_ozp) {
        	assemLiushuiZP($xj_array,$_ozp->amount,$inputjie,$inputdai,$sk_msk->acount,$date,$_mdb->kingdee_shopnum,$_ozp->real_name);
        }      
        //套餐卡支票收入
        $szp_amount=DB::select("SELECT sum(scpr.amount) as amount ,u.real_name FROM
            service_card_order_pay_record scpr
            LEFT JOIN service_card_order sco ON sco.order_id = scpr.order_id
            left join users u on u.id=sco.user_id
            WHERE sco.`status` IN (1018, 1017, 1217, 1317, 1417)
            AND sco.pay_time BETWEEN ? AND ? AND sco.shop_id = ?
            AND scpr.pay_type =11
            AND scpr.`status` = 1 group by sco.order_id", array($begin_date, $end_date,$_mdb->id));
        foreach ($szp_amount as $_szp) {
        	assemLiushuiZP($xj_array,$_szp->amount,$inputjie,$inputdai,$sk_msk->acount,$date,$_mdb->kingdee_shopnum,$_szp->real_name);
        }
        unset($ozp_amount);
        unset($szp_amount);
        //支票收入完毕
        //现金支票的退款
        //订单现金退款的记录
        $ro_amount=DB::select("SELECT o.car_no,ro.implement_time,sum(ror.amount) amount
			FROM return_order ro
			LEFT JOIN return_order_record ror ON ro.id = ror.return_order_id
			LEFT JOIN orders o ON o.order_id = ro.order_id
			WHERE
				ro.implement_time BETWEEN ? AND ? and  ro.`status` =1 AND o.shop_id = ?
			AND ror.pay_type IN (1,2,3,5,7,11) group by ro.id", array($begin_date, $end_date,$_mdb->id));
        foreach ($ro_amount as $_ro) {
        	assemLiushuiXJReturn($xj_array,$_ro->amount,$inputjie,$inputdai,$xj_msk->acount,$date,$_mdb->kingdee_shopnum,$_ro->car_no);
        }
        //洗车现金收入退款
        $rc_amount=DB::select("SELECT sum(rwr.amount) amount,cwo.car_no,rco.implement_time
			FROM
				return_carwash_order rco 
			LEFT JOIN return_washorder_record rwr ON rwr.return_washorder_id= rco.id
			LEFT JOIN car_wash_order cwo ON cwo.order_id = rco.order_id
			WHERE
				rco.`status` =1
				AND rco.implement_time BETWEEN ?
				AND ?
				AND cwo.shop_id = ?
				AND rwr.pay_type IN (1,2,3,7) group by rco.id", array($begin_date, $end_date,$_mdb->id));
        foreach ($rc_amount as $_rc) {
        	assemLiushuiXJReturn($xj_array,$_rc->amount,$inputjie,$inputdai,$xj_msk->acount,$date,$_mdb->kingdee_shopnum,$_rc->car_no);
        }
        // //套餐卡现金收入退款        
        $rs_amount=DB::select("SELECT sum(rscr.amount) amount,sco.car_no,rsco.implement_time
			FROM return_service_card_order rsco
			LEFT JOIN return_service_card_record rscr ON rsco.id = rscr.return_order_id
			LEFT JOIN service_card_order sco ON sco.order_id = rsco.order_id
			WHERE rsco.`status` =1
			AND rsco.implement_time BETWEEN ? AND ? AND sco.shop_id = ?
			AND rscr.pay_type IN (1,2,3,5,7,11) group by rsco.id", array($begin_date,$end_date,$_mdb->id));
        foreach ($rs_amount as $_rs) {
        	assemLiushuiXJReturn($xj_array,$_rs->amount,$inputjie,$inputdai,$xj_msk->acount,$date,$_mdb->kingdee_shopnum,$_rs->car_no);
        }
        unset($ro_amount);
        unset($rc_amount);
        unset($rs_amount);
        ////现金支票的退款
        $fianly=array_merge($fianly, $xj_array);
        // assembleNotsort($data,$xj_array,$date,$_mdb);//====================================================================================
        unset($xj_array);
        //现金支票的退款完毕

        //冲减预存的逻辑
        //订单余额冲减收入
        $shop_array=$temp_shop_array;//用于门店的循环数组 获取该店可能用到别点的记录
        $o_amount=DB::select("SELECT opr.id,opr.amount,u.kingdee_code,u.phone,u.type,u.real_name,o.car_no
            FROM order_pay_record opr
            LEFT JOIN orders o ON o.order_id = opr.order_id
            LEFT JOIN users u ON u.id = o.pay_user_id
            WHERE o.`status` IN (1018, 1017, 1217, 1317, 1417)
            AND o.pay_time BETWEEN ? AND ?  AND o.shop_id = ?
            AND opr.pay_type = 8
            AND opr.`status` = 1", array($begin_date, $end_date,$_mdb->id));
        foreach ($o_amount as $_oa) {
            $urr_amount=DB::select("SELECT uad.acount_type,aurr.shop_id,ms.kingdee_shopnum,ms.kingdee_bxcode,ms.name,sum(aursr.amount) as amount,aursr.implement_time
            FROM acount_user_recharge_spend_record aursr
            LEFT JOIN acount_user_recharge_record aurr ON aurr.id = aursr.aurr_id
            LEFT JOIN maintenance_shops ms on ms.id= aurr.shop_id
            LEFT JOIN user_account_details uad on uad.account_no=aurr.account_no
            WHERE aursr.buss_id = ? AND aursr.type = 1 group by uad.acount_type,aurr.shop_id", array($_oa->id));//获取这笔支付的完成需要几笔 充值支付记录
            $cur_type=$_oa->type==1?"个人":"企业";
            $cur_name=$_oa->real_name;
            $cur_code=$_oa->kingdee_code;           
            $car_no=$_oa->car_no;
            $user_type=$_oa->type;
            $flag=getlase($shop_array,$urr_amount,$_mdb,$inputjie,$inputdai,$cur_type,$user_type,$cur_name,$car_no,$ower_msk,$cur_code,$date);
            if(!$flag){
                continue 2; //跳过该天所有门店的
            }           
        }
        //洗车余额冲减收入
        $c_amount=DB::select("SELECT cwopr.id,cwopr.amount,u.kingdee_code,u.phone,u.type,u.real_name,cwo.car_no
            FROM car_wash_order_pay_record cwopr
            LEFT JOIN car_wash_order cwo ON cwo.order_id = cwopr.order_id
            LEFT JOIN users u ON u.id = cwo.user_id
            WHERE cwo.`status` IN (1018, 1017, 1217, 1317, 1417)
            AND cwo.pay_time BETWEEN ? AND ?  AND cwo.shop_id = ?
            AND cwopr.pay_type = 8
            AND cwopr.`status` = 1", array($begin_date, $end_date,$_mdb->id));
        foreach ($c_amount as $_ca) {
            $urr_amount=DB::select("SELECT uad.acount_type,aurr.shop_id,ms.kingdee_shopnum,ms.kingdee_bxcode,ms.name,sum(aursr.amount) as amount,aursr.implement_time
            FROM acount_user_recharge_spend_record aursr
            LEFT JOIN acount_user_recharge_record aurr ON aurr.id = aursr.aurr_id
            LEFT JOIN maintenance_shops ms on ms.id= aurr.shop_id
            LEFT JOIN user_account_details uad on uad.account_no=aurr.account_no
            WHERE aursr.buss_id = ? AND aursr.type = 2 group by uad.acount_type,aurr.shop_id", array($_ca->id));//获取这笔支付的完成需要几笔 充值支付记录
            $cur_type=$_ca->type==1?"个人":"企业";
            $cur_name=$_ca->real_name;
            $cur_code=$_ca->kingdee_code;           
            $car_no=$_ca->car_no;
            $user_type=$_ca->type;
            $flag=getlase($shop_array,$urr_amount,$_mdb,$inputjie,$inputdai,$cur_type,$user_type,$cur_name,$car_no,$ower_msk,$cur_code,$date);
            if(!$flag){
                continue 2; //跳过该天所有门店的
            }           
        }
        //套餐卡余额冲减收入
        $s_amount=DB::select("SELECT scopr.id,scopr.amount,u.kingdee_code,u.phone,u.type,u.real_name,sco.car_no
            FROM service_card_order_pay_record scopr
            LEFT JOIN service_card_order sco ON sco.order_id = scopr.order_id
            LEFT JOIN users u ON u.id = sco.user_id
            WHERE sco.`status` IN (1018, 1017, 1217, 1317, 1417)
            AND sco.pay_time BETWEEN ? AND ?  AND sco.shop_id = ?
            AND scopr.pay_type = 8
            AND scopr.`status` = 1", array($begin_date, $end_date,$_mdb->id));
        foreach ($s_amount as $_sa) {
            $urr_amount=DB::select("SELECT uad.acount_type,aurr.shop_id,ms.kingdee_shopnum,ms.kingdee_bxcode,ms.name,sum(aursr.amount) as amount,aursr.implement_time
            FROM acount_user_recharge_spend_record aursr
            LEFT JOIN acount_user_recharge_record aurr ON aurr.id = aursr.aurr_id
            LEFT JOIN maintenance_shops ms on ms.id= aurr.shop_id
            LEFT JOIN user_account_details uad on uad.account_no=aurr.account_no
            WHERE aursr.buss_id = ? AND aursr.type = 8 group by uad.acount_type,aurr.shop_id", array($_sa->id));//获取这笔支付的完成需要几笔 充值支付记录
            $cur_type=$_sa->type==1?"个人":"企业";
            $cur_name=$_sa->real_name;
            $cur_code=$_sa->kingdee_code;           
            $car_no=$_sa->car_no;
            $user_type=$_sa->type;
            $flag=getlase($shop_array,$urr_amount,$_mdb,$inputjie,$inputdai,$cur_type,$user_type,$cur_name,$car_no,$ower_msk,$cur_code,$date);
            if(!$flag){
                continue 2; //跳过该天所有门店的
            }           
        }   
        // //应收收回余额冲减收入
        $receive_amount=DB::select("SELECT rpr.id,rpr.amount,u.kingdee_code,u.phone,u.type,u.real_name,'' as car_no
            FROM receivable_pay_record rpr
            LEFT JOIN receivable r ON r.receivable_id = rpr.receivable_id
            LEFT JOIN users u ON u.id = r.company_id
            WHERE  r.verify_time BETWEEN ? AND ?  AND r.shop_id = ?
            and r.`verify_status`=1
            and r.`status`=1 
            AND rpr.pay_type = 8
            AND rpr.`status` = 1", array($begin_date,$end_date,$_mdb->id));
        foreach ($receive_amount as $_ra) {
            $urr_amount=DB::select("SELECT uad.acount_type,aurr.shop_id,ms.kingdee_shopnum,ms.kingdee_bxcode,ms.name,sum(aursr.amount) as amount,aursr.implement_time
            FROM acount_user_recharge_spend_record aursr
            LEFT JOIN acount_user_recharge_record aurr ON aurr.id = aursr.aurr_id
            LEFT JOIN maintenance_shops ms on ms.id= aurr.shop_id
            LEFT JOIN user_account_details uad on uad.account_no=aurr.account_no
            WHERE aursr.buss_id = ? AND aursr.type IN(5,7) group by uad.acount_type,aurr.shop_id", array($_ra->id));//获取这笔支付的完成需要几笔 充值支付记录
            $cur_type=$_ra->type==1?"个人":"企业";
            $cur_name=$_ra->real_name;
            $cur_code=$_ra->kingdee_code;           
            $car_no=$_ra->car_no;
            $user_type=$_ra->type;
            $flag=getlaseRec($shop_array,$urr_amount,$_mdb,$inputjie,$inputdai,$cur_type,$user_type,$cur_name,$car_no,$ower_msk,$cur_code,$date);
            if(!$flag){
                continue 2; //
            }           
        }
        unset($o_amount);
        unset($c_amount);
        unset($s_amount);
        unset($receive_amount);
        foreach($shop_array as &$ot){
            $temp_shop=&$ot;
            $depe_temp_bx=null;
            $depe_temp_xj=null;
            foreach($temp_shop["bx"] as $key=>$t){
                if($t['FAccountID']=='5101'&&$t['FExplanation'] =="冲减保险预收款收入"){
                    if(is_null($depe_temp_bx)){
                        $depe_temp_bx=$t;
                    }else{
                        $depe_temp_bx["FAmountFor"]+=$t['FAmountFor'];
                        $depe_temp_bx["FAmount"]+=$t['FAmount'];
                    }
                    unset($temp_shop["bx"][$key]);
                }
            }
            foreach ($temp_shop["xj"] as $key=>$t) {
                if($t['FAccountID']=='5101'&&$t['FExplanation'] =="冲减预收款收入"){
                    if(is_null($depe_temp_xj)){
                        $depe_temp_xj=$t;
                    }else{
                        $depe_temp_xj["FAmountFor"]+=$t['FAmountFor'];
                        $depe_temp_xj["FAmount"]+=$t['FAmount'];
                    }
                    unset($temp_shop["xj"][$key]);
                }
            }
            if(!is_null($depe_temp_bx)){
                $temp_shop["bx"][]=$depe_temp_bx;
            }
            if(!is_null($depe_temp_xj)){
                $temp_shop["xj"][]=$depe_temp_xj;
            }
            $ot=$temp_shop;        
        }
        $fianly=array_merge($fianly,$shop_array[$_mdb->id]["bx"]);
        $fianly=array_merge($fianly,$shop_array[$_mdb->id]["xj"]);
        unset($shop_array[$_mdb->id]);
        // foreach ($shop_array as $key=>$sa) {
        //     if(sizeof($sa)==0){     
        //         continue;
        //     }
        //     $temp_mdb=MaintenanceShops::find($key);
        //     if(!$temp_mdb->kingdee_groupid){
        //         Log::error("门店 ".$temp_mdb->id."金蝶科目部门异常");
        //         continue 2; //跳过该天所有门店的
        //     } 
        //     assembleNotsort($data,$sa,$date,$temp_mdb);//==================================================================
        // }
        //应收收回的逻辑
        $receive_array=array();
        $receive_info=DB::select("SELECT rpr.pay_type,rpr.amount,u.id,u.kingdee_code,u.phone,u.type,u.real_name
            FROM receivable r
            LEFT JOIN receivable_pay_record rpr ON rpr.receivable_id = r.receivable_id
            LEFT JOIN users u ON u.id = r.company_id
            WHERE r.verify_time BETWEEN ? AND ?
            AND r.verify_status = 1
            AND r.shop_id = ?
            AND r.`status` = 1
            AND rpr.pay_type in(1,11,15)
            AND rpr.`status` = 1", array($begin_date,$end_date,$_mdb->id));
        foreach ($receive_info as $_arr) {
            if(in_array($_arr->pay_type,array(11,15))){//如果是 汇款+支票
                $temp_jie_code=$sk_msk->acount;
            }else{//其他都算现金
                $temp_jie_code=$xj_msk->acount;
            }
            assemLiushuiSH($receive_array,$_arr->amount,$inputjie,$inputdai,$temp_jie_code,$date,$_arr->real_name,$_arr->kingdee_code,$_mdb->kingdee_shopnum);
        } 

        $receive_info=DB::select("SELECT rpr.pay_type,rpr.amount,u.id,u.kingdee_code,u.phone,u.type,u.real_name,wr.kingdee_code as yh_kingdee_code
            FROM receivable r
            LEFT JOIN receivable_pay_record rpr ON rpr.receivable_id = r.receivable_id
            LEFT JOIN wpos_record wr on wr.type=4 and wr.buss_id=rpr.id
            LEFT JOIN users u ON u.id = r.company_id
            WHERE r.verify_time BETWEEN ? AND ?
            AND r.verify_status = 1
            AND r.shop_id = ?
            AND r.`status` = 1
            AND rpr.pay_type in(2,3,7)
            AND rpr.`status` = 1", array($begin_date,$end_date,$_mdb->id));
        foreach ($receive_info as $_arr) {
            if($_arr->yh_kingdee_code){//如果是 配置了金蝶代码的
                $temp_jie_code=$_arr->yh_kingdee_code;
            }else{//其他都算现金
                $temp_jie_code=$xj_msk->acount;
            }
            assemLiushuiSH($receive_array,$_arr->amount,$inputjie,$inputdai,$temp_jie_code,$date,$_arr->real_name,$_arr->kingdee_code,$_mdb->kingdee_shopnum);
        } 
        $fianly=array_merge($fianly, $receive_array);
        // assembleNotsort($data,$receive_array,$date,$_mdb);//==================================================================
        unset($receive_array);
        
        //刷卡收入开始
        //订单刷卡收入
        $sk_array=array();
        $temp_amount=array();
        $o_amount=DB::select("SELECT sum(opr.amount) as amount,wr.kingdee_code FROM
            order_pay_record opr
            LEFT JOIN wpos_record wr on  wr.type=2 and wr.buss_id=opr.id  
            LEFT JOIN orders o ON o.order_id = opr.order_id
            WHERE  o.`status` IN (1018, 1017, 1217, 1317, 1417)
            AND o.pay_time BETWEEN ? AND ? AND o.shop_id = ?
            AND opr.pay_type in(2,3,5,7)
            AND opr.`status` = 1 group by wr.kingdee_code", array($begin_date, $end_date,$_mdb->id));
        foreach ($o_amount as $_o) {
            if($_o->kingdee_code){//如果配置了金蝶代码
                if(isset($temp_amount[$_o->kingdee_code])){
                    $temp_amount[$_o->kingdee_code]=$temp_amount[$_o->kingdee_code]+$_o->amount;
                }else{
                    $temp_amount[$_o->kingdee_code]=$_o->amount;
                }
            }else{//如果没有配置金蝶代码 按照门店的现金
                if(isset($temp_amount[$xj_msk->acount])){
                    $temp_amount[$xj_msk->acount]=$temp_amount[$xj_msk->acount]+$_o->amount;
                }else{
                    $temp_amount[$xj_msk->acount]=$_o->amount;
                }
            }
        }
        //洗车刷卡收入
        $c_amount=DB::select("SELECT  sum(cpr.amount) as amount,wr.kingdee_code FROM
            car_wash_order_pay_record cpr
            LEFT JOIN wpos_record wr on  wr.type=3 and wr.buss_id=cpr.id  
            LEFT JOIN car_wash_order co ON co.order_id = cpr.order_id
            WHERE  co.`status` IN (1018, 1017, 1217, 1317, 1417)
            AND co.pay_time BETWEEN ? AND ? AND co.shop_id = ?
            AND cpr.pay_type in(2,3,7)
            AND cpr.`status` = 1 group by wr.kingdee_code ", array($begin_date, $end_date,$_mdb->id));
        foreach ($c_amount as $_o) {
            if($_o->kingdee_code){//如果配置了金蝶代码
                if(isset($temp_amount[$_o->kingdee_code])){
                    $temp_amount[$_o->kingdee_code]=$temp_amount[$_o->kingdee_code]+$_o->amount;
                }else{
                    $temp_amount[$_o->kingdee_code]=$_o->amount;
                }
            }else{//如果没有配置金蝶代码 按照门店的现金
                if(isset($temp_amount[$xj_msk->acount])){
                    $temp_amount[$xj_msk->acount]=$temp_amount[$xj_msk->acount]+$_o->amount;
                }else{
                    $temp_amount[$xj_msk->acount]=$_o->amount;
                }
            }
        }
        //套餐卡刷卡收入
        $s_amount=DB::select("SELECT sum(scpr.amount) as amount,wr.kingdee_code FROM
            service_card_order_pay_record scpr
            LEFT JOIN wpos_record wr on  wr.type=5 and wr.buss_id=scpr.id  
            LEFT JOIN service_card_order sco ON sco.order_id = scpr.order_id
            WHERE sco.`status` IN (1018, 1017, 1217, 1317, 1417)
            AND sco.pay_time BETWEEN ? AND ? AND sco.shop_id = ?
            AND scpr.pay_type in(2,3,5,7)
            AND scpr.`status` = 1 group by wr.kingdee_code", array($begin_date, $end_date,$_mdb->id));
        foreach ($s_amount as $_o) {
            if($_o->kingdee_code){//如果配置了金蝶代码
                if(isset($temp_amount[$_o->kingdee_code])){
                    $temp_amount[$_o->kingdee_code]=$temp_amount[$_o->kingdee_code]+$_o->amount;
                }else{
                    $temp_amount[$_o->kingdee_code]=$_o->amount;
                }
            }else{//如果没有配置金蝶代码 按照门店的现金
                if(isset($temp_amount[$xj_msk->acount])){
                    $temp_amount[$xj_msk->acount]=$temp_amount[$xj_msk->acount]+$_o->amount;
                }else{
                    $temp_amount[$xj_msk->acount]=$_o->amount;
                }
            }
        }
        foreach ($temp_amount as $key => $value) {
           assemLiushuiSK($sk_array,$value,$inputjie,$inputdai,$key,$date,$_mdb->kingdee_shopnum); 
        }
        zuheSK($sk_array,$date,$_mdb->kingdee_shopnum);
        $fianly=array_merge($fianly, $sk_array);
        unset($sk_array);
        //刷卡收入完毕

        //现金预存
        $xi_array=array();
        $arr_array=DB::select("SELECT aurr.pay_type,aurr.amount,u.id,u.kingdee_code,u.phone,u.type,u.real_name
            FROM `acount_user_recharge_record` aurr
                left join users u on u.id=aurr.user_id
            WHERE aurr.implement_time BETWEEN ? AND ?
            AND aurr.pay_type IN (1, 2, 3, 7, 11)
            and aurr.shop_id=?
            AND aurr.`status` = 1", array($begin_date,$end_date,$_mdb->id));
        foreach ($arr_array as $_arr) {
            if(in_array($_arr->pay_type,array(11))){//如果是支票
                $temp_desc="支票";
                $temp_jie_code=$sk_msk->acount;
            }else{//其他都算现金
                $temp_desc="现金";
                $temp_jie_code=$xj_msk->acount;
            }
            if($_arr->type==1){//如果是 个人客户
                assemLiushuiXJGR($xi_array,$_arr->amount,$inputjie,$inputdai,$temp_jie_code,$date,$temp_desc,$_arr->phone,$_mdb->kingdee_shopnum);
            }else{//如果是 企业客户
                assemLiushuiXJQY($xi_array,$_arr->amount,$inputjie,$inputdai,$temp_jie_code,$date,$temp_desc,$_arr->real_name,$_arr->kingdee_code,$_mdb->kingdee_shopnum);
            }

        }     
        $fianly=array_merge($fianly, $xi_array);
        assembleNotsort($data,$fianly,$date,$_mdb);//==================================================================
        unset($xi_array);
        unset($fianly);
        



        //应收的账套  单独一张凭证
        //订单的应收
        $oys_amount=DB::select("SELECT sum(opr.amount) as amount ,u.id,u.real_name,o.car_no,u.kingdee_code FROM
            order_pay_record opr
            LEFT JOIN orders o ON o.order_id = opr.order_id 
            left join users u on u.id=o.pay_user_id
            WHERE  o.`status` IN (1018, 1017, 1217, 1317, 1417)
            AND o.pay_time BETWEEN ? AND ? AND o.shop_id = ?
            AND opr.pay_type =6
            AND opr.`status` = 1 group by o.order_id", array($begin_date, $end_date,$_mdb->id));  
        foreach ($oys_amount as $_oys) {
            if(!$_oys->kingdee_code){
                Log::error("客户 ".$_oys->id." 金蝶科目异常");
                continue 2;//有错误数据
            }
            assemLiushuiYS($ys_array,$_oys->amount,$inputjie,$inputdai,1131,$date,$_mdb->kingdee_shopnum,$_oys->kingdee_code,$_oys->car_no,$_oys->real_name);
        }
        //洗车单的应收
        $cys_amount=DB::select("SELECT sum(cwopr.amount) as amount ,u.id,u.real_name,cwr.car_no,u.kingdee_code FROM
            car_wash_order_pay_record cwopr
            LEFT JOIN car_wash_order cwr ON cwr.order_id = cwopr.order_id 
            left join users u on u.id=cwr.user_id
            WHERE  cwr.`status` IN (1018, 1017, 1217, 1317, 1417)
                        AND cwr.pay_time BETWEEN ? AND ? AND cwr.shop_id = ?
            AND cwopr.pay_type =6
            AND cwopr.`status` = 1 group by cwr.order_id", array($begin_date, $end_date,$_mdb->id));  
        foreach ($cys_amount as $_cys) {
            if(!$_cys->kingdee_code){
                Log::error("客户 ".$_cys->id." 金蝶科目异常");
                continue 2;//有错误数据
            }
            assemLiushuiYS($ys_array,$_cys->amount,$inputjie,$inputdai,1131,$date,$_mdb->kingdee_shopnum,$_cys->kingdee_code,$_cys->car_no,$_cys->real_name);
        }
        //套餐卡订单的应收
        $sys_amount=DB::select("SELECT sum(scopr.amount) as amount ,u.id,u.real_name,sco.car_no,u.kingdee_code  FROM
            service_card_order_pay_record scopr
            LEFT JOIN service_card_order sco ON sco.order_id = scopr.order_id 
            left join users u on u.id=sco.user_id
            WHERE  sco.`status` IN (1018, 1017, 1217, 1317, 1417)
            AND sco.pay_time BETWEEN ? AND ? AND sco.shop_id = ?
            AND scopr.pay_type =6
            AND scopr.`status` = 1 group by sco.order_id ", array($begin_date, $end_date,$_mdb->id));  
        foreach ($sys_amount as $_sys) {
            if(!$_sys->kingdee_code){
                Log::error("客户 ".$_sys->id." 金蝶科目异常");
                continue 2;//有错误数据
            }
            assemLiushuiYS($ys_array,$_sys->amount,$inputjie,$inputdai,1131,$date,$_mdb->kingdee_shopnum,$_sys->kingdee_code,$_sys->car_no,$_sys->real_name);
        }
        //应收完毕
        //应收退款
        //订单应收退款
        $roys_amount=DB::select("SELECT o.car_no,ro.implement_time,sum(ror.amount) amount ,u.id,u.real_name,u.kingdee_code
            FROM return_order ro
            LEFT JOIN return_order_record ror ON ro.id = ror.return_order_id
            LEFT JOIN orders o ON o.order_id = ro.order_id
            left join users u on u.id=o.pay_user_id
            WHERE
                ro.implement_time BETWEEN ? AND ? and  ro.`status` =1 AND o.shop_id = ?
            AND ror.pay_type =6 group by ro.id", array($begin_date, $end_date,$_mdb->id));
        foreach ($roys_amount as $_roys) {
            assemLiushuiYS($ys_array,-$_roys->amount,$inputjie,$inputdai,1131,$date,$_mdb->kingdee_shopnum,$_roys->kingdee_code,$_roys->car_no,$_roys->real_name);
        }
        //洗车应收退款
        $rcys_amount=DB::select("SELECT sum(rwr.amount) amount,cwo.car_no,rco.implement_time,u.id,u.real_name,u.kingdee_code
            FROM
                return_carwash_order rco 
            LEFT JOIN return_washorder_record rwr ON rwr.return_washorder_id= rco.id
            LEFT JOIN car_wash_order cwo ON cwo.order_id = rco.order_id
            left join users u on u.id=cwo.user_id
            WHERE
                rco.`status` =1
                AND rco.implement_time BETWEEN ?
                AND ?
                AND cwo.shop_id = ?
                AND rwr.pay_type =6 group by rco.id", array($begin_date, $end_date,$_mdb->id));
        foreach ($rcys_amount as $_rcys) {
            assemLiushuiYS($ys_array,-$_rcys->amount,$inputjie,$inputdai,1131,$date,$_mdb->kingdee_shopnum,$_rcys->kingdee_code,$_rcys->car_no,$_rcys->real_name);
        }
        // // //套餐卡应收退款        
        $rsys_amount=DB::select("SELECT sum(rscr.amount) amount,sco.car_no,rsco.implement_time,u.id,u.real_name,u.kingdee_code
            FROM return_service_card_order rsco
            LEFT JOIN return_service_card_record rscr ON rsco.id = rscr.return_order_id
            LEFT JOIN service_card_order sco ON sco.order_id = rsco.order_id
            left join users u on u.id=sco.user_id
            WHERE rsco.`status` =1
            AND rsco.implement_time BETWEEN ? AND ? AND sco.shop_id = ?
            AND rscr.pay_type  =6 group by rsco.id", array($begin_date,$end_date,$_mdb->id));
        foreach ($rsys_amount as $_rsys) {
            assemLiushuiYS($ys_array,-$_rsys->amount,$inputjie,$inputdai,1131,$date,$_mdb->kingdee_shopnum,$_rsys->kingdee_code,$_rsys->car_no,$_rsys->real_name);
        }
        //应收的退款完毕
        //应收完毕
        zuhe($ys_array,$date);
        assembleNotsort($data,$ys_array,$date,$_mdb);//====================================================================================
        unset($ys_array);

        foreach ($shop_array as $key=>$sa) {
            if((sizeof($sa["bx"])+sizeof($sa["xj"]))==0){     
                continue;
            }
            $temp_mdb=MaintenanceShops::find($key);
            if(!$temp_mdb->kingdee_groupid){
                Log::error("门店 ".$temp_mdb->id."金蝶科目部门异常");
                continue 2; //跳过该天所有门店的
            } 
            assembleNotsort($data,$sa["bx"],$date,$temp_mdb);//==================================================================
            assembleNotsort($data,$sa["xj"],$date,$temp_mdb);
        }

        // Log::info($data);
        if(sizeof($data["mainVoucher"])>0){  
            getMaintain($_mdb->id,$data,$date);
            Log::info("门店".$_mdb->id." 日期 ".$date.'完毕');
            echo "门店".$_mdb->id." 日期 ".$date.'完毕';
        }else{
            Log::info("门店".$_mdb->id." 日期 ".$date.'完毕');
            echo "门店".$_mdb->id." 日期 ".$date.'完毕';
        }  
    }
}
function zuhe(&$array,$date){
    //合并 5501 和 科目备注相同的科目
    $date=date('m-d',strtotime($date));   
    if(sizeof($array)==0){
        return $array;
    }
    $temp_shop=$array;
    $depe_temp_ys=null;
    $depe_temp_xj=null;
    foreach($temp_shop as $key=>$t){
        if($t['FAccountID']=='5101'&&$t['FExplanation'] ==$date." 应收收入"){
            if(is_null($depe_temp_ys)){
                $depe_temp_ys=$t;
            }else{
                $depe_temp_ys["FAmountFor"]+=$t['FAmountFor'];
                $depe_temp_ys["FAmount"]+=$t['FAmount'];
            }
            unset($temp_shop[$key]);
        }else if($t['FAccountID']=='5101'&&$t['FExplanation'] ==$date." 现金收入"){
            if(is_null($depe_temp_xj)){
                $depe_temp_xj=$t;
            }else{
                $depe_temp_xj["FAmountFor"]+=$t['FAmountFor'];
                $depe_temp_xj["FAmount"]+=$t['FAmount'];
            }
            unset($temp_shop[$key]);
        }
    }
    if(!is_null($depe_temp_ys)){
        $temp_shop[]=$depe_temp_ys;
    }
    if(!is_null($depe_temp_xj)){
        $temp_shop[]=$depe_temp_xj;
    }
    $array=$temp_shop;        
}

function zuheSK(&$array,$date,$shop_code){
    //合并 5501 和 科目备注相同的科目
    $date=date('m-d',strtotime($date));   
    if(sizeof($array)==0){
        return $array;
    }
    $temp_shop=$array;
    $depe_temp_xj=null;
    foreach($temp_shop as $key=>$t){
        if($t['FAccountID']=='5101'&&$t['FExplanation'] ==$date." 刷卡收入"){
            if(is_null($depe_temp_xj)){
                $depe_temp_xj=$t;
            }else{
                $depe_temp_xj["FAmountFor"]+=$t['FAmountFor'];
                $depe_temp_xj["FAmount"]+=$t['FAmount'];
            }
            unset($temp_shop[$key]);
        }
    }
    $temp_shop[] = array(
        "FAccountID"=>"5503.01",
        "FExplanation"=>"刷卡手续费",
        "FCurrencyID"=>1,
        "FExchangeRate"=>1,
        "FDC"=>1,
        "FAmountFor"=>0,
        "FAmount"=>0,
        "data"=>array(
            "012"=>$shop_code,
        ),
    );

    if(!is_null($depe_temp_xj)){
        $temp_shop[]=$depe_temp_xj;
    }
    $array=$temp_shop;        
}

/**
 * @Author   lyt                      <ahlyt@anhuibj.com>
 * @DateTime 2018-07-03T15:34:59+0800
 * 用于奖一堆凭证详情拼接成一个凭证 并最终加入 最终的凭证结构体中
 */
function assembleNotsort(&$finally,$value_array,$date,$_mdb){
    if(sizeof($value_array)==0){     
        return $finally;
    }
    $temp =array(
        "FDate"=>$date." 00:00:00",
        "FGroupID"=>$_mdb->kingdee_groupid,//来广营 6
        "FReference"=>null,
        "FAttachments"=>0,
        "FEntryCount"=>4,
        "FDebitTotal"=>0,
        "FCreditTotal"=>0,
        "FPreparerID"=>16428,//写死的制单人
        "FPosterID"=>-1,
        "FTranType"=>0,
        "FTransDate"=>$date." 00:00:00",
        "entryVoucher"=>array(
            
        )           
    );
    $FEntryCount=0;
    $FDebitTotal=0;
    $FCreditTotal=0;
    foreach ($value_array as $value) {  
        $FEntryCount++;
        $value["FAmountFor"]=round($value["FAmountFor"],2);
        $value["FAmount"]=round($value["FAmount"],2);
        if($value["FDC"]==1){
            $FDebitTotal+=$value["FAmountFor"];
        }else{
            $FCreditTotal+=$value["FAmountFor"];
        }                           
        $temp["entryVoucher"][]=$value;
    }
    $temp["FEntryCount"]=$FEntryCount;
    $temp["FDebitTotal"]=round($FDebitTotal,2);
    $temp["FCreditTotal"]=round($FCreditTotal,2);
    $finally["mainVoucher"][]=$temp;
    return $finally;
}

/**
 * @Author   lyt                      <ahlyt@anhuibj.com>
 * @DateTime 2018-07-03T15:34:59+0800
 * 用于奖一堆凭证详情拼数组 接成一个凭证数组 并最终加入 最终的凭证结构体中
 */

//预存的个人的
function assemLiushuiXJGR(&$data,$amount,$inputjie,$inputdai,$jie_account,$date,$pay_type,$phone,$shop_code){   
    if($amount>0){
        $date=date('m-d',strtotime($date));
        $inputjie["FAccountID"]=$jie_account;
        $inputjie["FExplanation"]=$date." ".$pay_type." 收入";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="2131";
        $inputdai["FExplanation"]=$date." ".$pay_type." 充值 ".$phone;  
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array();
        $datedai=array("001"=>184,"012"=>$shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}
//预存的企业的
function assemLiushuiXJQY(&$data,$amount,$inputjie,$inputdai,$jie_account,$date,$pay_type,$cur_name,$cur_code,$shop_code){   
    if($amount>0){
        $date=date('m-d',strtotime($date));
        $inputjie["FAccountID"]=$jie_account;
        $inputjie["FExplanation"]=$date." ".$pay_type." 收入";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="2131";
        $inputdai["FExplanation"]=$date." ".$cur_name."".$pay_type." 充值 ";  
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array();
        $datedai=array("001"=>$cur_code,"012"=>$shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}
function assemLiushuiXJ(&$data,$xj_amount,$inputjie,$inputdai,$jie_account,$date,$shop_code){	
	if($xj_amount>0){
        $date=date('m-d',strtotime($date));
        $inputjie["FAccountID"]=$jie_account;
        $inputjie["FExplanation"]=$date." 现金收入";
        $inputjie["FAmountFor"]=round($xj_amount/100,2);
        $inputjie["FAmount"]=round($xj_amount/100,2);

        $inputdai["FAccountID"]="5101";
        $inputdai["FExplanation"]=$date." 现金收入";  
        $inputdai["FAmountFor"]=round($xj_amount/100,2);
        $inputdai["FAmount"]=round($xj_amount/100,2);

        $datejie=array();
        $datedai=array("012"=>$shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
	}
}

function assemLiushuiSK(&$data,$xj_amount,$inputjie,$inputdai,$jie_account,$date,$shop_code){   
    if($xj_amount>0){
        $date=date('m-d',strtotime($date));
        $inputjie["FAccountID"]=$jie_account;
        $inputjie["FExplanation"]=$date." 刷卡收入";
        $inputjie["FAmountFor"]=round($xj_amount/100,2);
        $inputjie["FAmount"]=round($xj_amount/100,2);

        $inputdai["FAccountID"]="5101";
        $inputdai["FExplanation"]=$date." 刷卡收入";  
        $inputdai["FAmountFor"]=round($xj_amount/100,2);
        $inputdai["FAmount"]=round($xj_amount/100,2);

        $datejie=array();
        $datedai=array("012"=>$shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie; 
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}
function assemLiushuiXJReturn(&$data,$xj_amount,$inputjie,$inputdai,$jie_account,$date,$shop_code,$car_no){	
	if($xj_amount>0){
        $date=date('m-d',strtotime($date));
        $inputjie["FAccountID"]=$jie_account;
        $inputjie["FExplanation"]=$date." ".$car_no." 现金收入 退款";
        $inputjie["FAmountFor"]=-round($xj_amount/100,2);
        $inputjie["FAmount"]=-round($xj_amount/100,2);

        $inputdai["FAccountID"]="5101";
        $inputdai["FExplanation"]=$date." ".$car_no." 现金收入 退款";
        $inputdai["FAmountFor"]=-round($xj_amount/100,2);
        $inputdai["FAmount"]=-round($xj_amount/100,2);

        $datejie=array();
        $datedai=array("012"=>$shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
	}
}

function assemLiushuiZP(&$data,$xj_amount,$inputjie,$inputdai,$jie_account,$date,$shop_code,$cus_name){	
	if($xj_amount>0){
        $date=date('m-d',strtotime($date));
        $inputjie["FAccountID"]=$jie_account;
        $inputjie["FExplanation"]=$cus_name." 支票收入";
        $inputjie["FAmountFor"]=round($xj_amount/100,2);
        $inputjie["FAmount"]=round($xj_amount/100,2);

        $inputdai["FAccountID"]="5101";
        $inputdai["FExplanation"]=$cus_name." 支票收入";  
        $inputdai["FAmountFor"]=round($xj_amount/100,2);
        $inputdai["FAmount"]=round($xj_amount/100,2);

        $datejie=array();
        $datedai=array("012"=>$shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
	}
}
//应收都用这个 此处 金额会传负值所以不能添加 金额大于零的判断
function assemLiushuiYS(&$data,$ys_amount,$inputjie,$inputdai,$jie_account,$date,$shop_code,$cus_code,$car_no,$cus_name){	
    $date=date('m-d',strtotime($date));
    $inputjie["FAccountID"]=$jie_account;
    $inputjie["FExplanation"]=$date." 应收 ".$cus_name."/".$car_no." 收入".($ys_amount<0?" 退款":"");
    $inputjie["FAmountFor"]=round($ys_amount/100,2);
    $inputjie["FAmount"]=round($ys_amount/100,2);

    $inputdai["FAccountID"]="5101";
    $inputdai["FExplanation"]=$date." 应收收入";  
    $inputdai["FAmountFor"]=round($ys_amount/100,2);
    $inputdai["FAmount"]=round($ys_amount/100,2);

    $datejie=array("001"=>$cus_code,"012"=>$shop_code);
    $datedai=array("012"=>$shop_code);
    
    $inputjie["data"]=$datejie;
    $data[]=$inputjie;      
    $inputdai["data"]=$datedai;
    $data[]=$inputdai;
}



//用于收回的
function assemLiushuiSH(&$data,$amount,$inputjie,$inputdai,$jie_account,$date,$cur_name,$cur_code,$shop_code){   
    if($amount>0){
        $date=date('m-d',strtotime($date));
        $inputjie["FAccountID"]=$jie_account;
        $inputjie["FExplanation"]=$date." 收回 ".$cur_name." 欠款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="1131";
        $inputdai["FExplanation"]=$date." 收回 ".$cur_name." 欠款";
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array();
        $datedai=array("001"=>$cur_code,"012"=>$shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}
//用于冲减本店的保险预存款
function assemLiushuiBX(&$data,$amount,$inputjie,$inputdai,$cur_type,$car_no,$shop_code,$shop_bxcode){   
    if($amount>0){
        // $date=date('m-d',strtotime($date));
        $inputjie["FAccountID"]="2181.12";
        $inputjie["FExplanation"]=$cur_type." ".$car_no." 保险预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="5101";
        $inputdai["FExplanation"]="冲减保险预收款收入";  
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("012"=>$shop_code,"017"=>$shop_bxcode);
        $datedai=array("012"=>$shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}
//用于应收 冲减本店的保险预存款
function assemLiushuiBXRe(&$data,$amount,$inputjie,$inputdai,$cur_type,$cur_code,$shop_code,$shop_bxcode,$cur_name){   
    if($amount>0){
        $inputjie["FAccountID"]="2181.12";
        $inputjie["FExplanation"]=$cur_type." ".$cur_name." 冲减保险预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="1131";
        $inputdai["FExplanation"]="收回".$cur_name."欠款 冲减保险预收款";  
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("012"=>$shop_code,"017"=>$shop_bxcode);
        $datedai=array("001"=>$cur_code,"012"=>$shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}
//用于使用别店预存款本店的账套
function assemLiushuiOWerBX(&$data,$amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$other_shop_name,$ower_shop_code,$other_jie_code){   
    if($amount>0){
        $date=date('m-d',strtotime($date));
        $inputjie["FAccountID"]=$other_jie_code;
        $inputjie["FExplanation"]=$date." ".$cur_type." ".$car_no." 冲减 ".$other_shop_name." 保险预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="5101";
        $inputdai["FExplanation"]="冲减保险预收款收入";  
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("012"=>$ower_shop_code);
        $datedai=array("012"=>$ower_shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}
//用于收回使用别店预存款本店的账套
function assemLiushuiOWerBXRe(&$data,$amount,$inputjie,$inputdai,$date,$cur_type,$cur_code,$car_no,$other_shop_name,$ower_shop_code,$other_jie_code){  
    if($amount>0){
        $inputjie["FAccountID"]=$other_jie_code;
        $inputjie["FExplanation"]="收回".$cur_name."欠款 冲减".$other_shop_name."保险预收款"; 
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="1131";
        $inputdai["FExplanation"]="收回".$cur_name."欠款 冲减".$other_shop_name."保险预收款"; 
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("012"=>$ower_shop_code);
        $datedai=array("001"=>$cur_code,"012"=>$ower_shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}
//用于使用别店预存款别店的账套
function assemLiushuiOtherBX(&$other_data,$amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$ower_shop_name,$other_shop_code,$ower_jie_code,$other_shop_bxcode){   
    if($amount>0){
        $date=date('m-d',strtotime($date));
        $inputjie["FAccountID"]="2181.12"; 
        $inputjie["FExplanation"]=$date." ".$ower_shop_name."冲减".$cur_type." ".$car_no." 保险预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]=$ower_jie_code;
        $inputdai["FExplanation"]=$date." ".$ower_shop_name."冲减".$cur_type." ".$car_no." 保险预收款";
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("012"=>$other_shop_code,"017"=>$other_shop_bxcode);
        $datedai=array("012"=>$other_shop_code);
        
        $inputjie["data"]=$datejie;
        $other_data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $other_data[]=$inputdai;
    }
}

//用于收回 使用别店预存款别店的账套
function assemLiushuiOtherBXRe(&$other_data,$amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$ower_shop_name,$other_shop_code,$ower_jie_code,$other_shop_bxcode){   
    if($amount>0){
        $date=date('m-d',strtotime($date));
        $inputjie["FAccountID"]="2181.12"; 
        $inputjie["FExplanation"]=$date." ".$ower_shop_name."冲减".$cur_type." ".$car_no." 冲减保险预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]=$ower_jie_code;
        $inputdai["FExplanation"]=$date." ".$ower_shop_name."冲减".$cur_type." ".$car_no." 冲减保险预收款";
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("012"=>$other_shop_code,"017"=>$other_shop_bxcode);
        $datedai=array("012"=>$other_shop_code);
        
        $inputjie["data"]=$datejie;
        $other_data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $other_data[]=$inputdai;
    }
}
//用于冲减本店的个人现金预存款
function assemLiushuiXHGR(&$data,$amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$shop_code){   
    if($amount>0){
        $date=date('m-d',strtotime($date));
        $inputjie["FAccountID"]="2131";
        $inputjie["FExplanation"]=$date." ".$cur_type." ".$car_no." 冲减预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="5101";
        $inputdai["FExplanation"]="冲减预收款收入";  
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("001"=>184,"012"=>$shop_code);
        $datedai=array("012"=>$shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}

//用于使用别店现金个人本店的账套
function assemLiushuiOWerXHGR(&$data,$amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$other_shop_name,$ower_shop_code,$other_jie_code){   
    if($amount>0){
        $date=date('m-d',strtotime($date));
        $inputjie["FAccountID"]=$other_jie_code;
        $inputjie["FExplanation"]=$date." ".$cur_type." ".$car_no." 冲减 ".$other_shop_name." 预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="5101";
        $inputdai["FExplanation"]="冲减预收款收入";  
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("012"=>$ower_shop_code);
        $datedai=array("012"=>$ower_shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}
//用于使用别店现金个人别店的账套
function assemLiushuiOtherXHGR(&$other_data,$amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$ower_shop_name,$other_shop_code,$ower_jie_code){   
    if($amount>0){
        $date=date('m-d',strtotime($date));
        $inputjie["FAccountID"]="2131"; 
        $inputjie["FExplanation"]=$date." ".$ower_shop_name."冲减".$cur_type." ".$car_no." 预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]=$ower_jie_code;//使用门店的 往来代码
        $inputdai["FExplanation"]=$date." ".$ower_shop_name."冲减".$cur_type." ".$car_no." 预收款";
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("001"=>184,"012"=>$other_shop_code);
        $datedai=array("012"=>$other_shop_code);//预存门店的 门店代码
        
        $inputjie["data"]=$datejie;
        $other_data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $other_data[]=$inputdai;
    }
}


//用于冲减本店的企业现金预存款
function assemLiushuiXHQY(&$data,$amount,$inputjie,$inputdai,$date,$cur_name,$cur_code,$shop_code){   
    if($amount>0){
        $date=date('m-d',strtotime($date));
        $inputjie["FAccountID"]="2131";
        $inputjie["FExplanation"]=$date." ".$cur_name." 冲减预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="5101";
        $inputdai["FExplanation"]="冲减预收款收入";  
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("001"=>$cur_code,"012"=>$shop_code);
        $datedai=array("012"=>$shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}
//用于应收 冲减本店企业现金预存款
function assemLiushuiXHQYRe(&$data,$amount,$inputjie,$inputdai,$date,$cur_name,$cur_code,$shop_code){   
    if($amount>0){
        $date=date('m-d',strtotime($date));
        $inputjie["FAccountID"]="2131";
        $inputjie["FExplanation"]=$date." ".$cur_name." 冲减预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="1131";
        $inputdai["FExplanation"]="收回".$cur_name."欠款 冲减预收款";
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("001"=>$cur_code,"012"=>$shop_code);
        $datedai=array("001"=>$cur_code,"012"=>$shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}

//用于使用别店现金企业本店的账套
function assemLiushuiOWerXHQY(&$data,$amount,$inputjie,$inputdai,$date,$cur_name,$car_no,$other_shop_name,$ower_shop_code,$other_jie_code){   
    if($amount>0){
        $date=date('m-d',strtotime($date));
        $inputjie["FAccountID"]=$other_jie_code;
        $inputjie["FExplanation"]=$date." ".$cur_name." ".$car_no." 冲减 ".$other_shop_name." 预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="5101";
        $inputdai["FExplanation"]="冲减预收款收入";  
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("012"=>$ower_shop_code);
        $datedai=array("012"=>$ower_shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}
//用于应收 使用别店现金企业本店的账套
function assemLiushuiOWerXHQYRe(&$data,$amount,$inputjie,$inputdai,$date,$cur_name,$cur_code,$car_no,$other_shop_name,$ower_shop_code,$other_jie_code){   
    if($amount>0){
        // $date=date('m-d',strtotime($date);
        $inputjie["FAccountID"]=$other_jie_code;
        $inputjie["FExplanation"]="收回".$cur_name."欠款 冲减".$other_shop_name."预收款"; 
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="1131";
        $inputdai["FExplanation"]="收回".$cur_name."欠款 冲减".$other_shop_name."预收款"; 
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("012"=>$ower_shop_code);
        $datedai=array("001"=>$cur_code,"012"=>$ower_shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}
//用于使用别店现金企业别店的账套
function assemLiushuiOtherXHQY(&$other_data,$amount,$inputjie,$inputdai,$date,$cur_name,$cur_code,$car_no,$ower_shop_name,$other_shop_code,$ower_jie_code){   
    if($amount>0){
        $date=date('m-d',strtotime($date));
        $inputjie["FAccountID"]="2131"; 
        $inputjie["FExplanation"]=$date." ".$ower_shop_name."冲减".$cur_name." ".$car_no." 预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]=$ower_jie_code;//使用门店的 往来代码
        $inputdai["FExplanation"]=$date." ".$ower_shop_name."冲减".$cur_name." ".$car_no." 预收款";
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("001"=>$cur_code,"012"=>$other_shop_code);
        $datedai=array("012"=>$other_shop_code);//预存门店的 门店代码
        
        $inputjie["data"]=$datejie;
        $other_data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $other_data[]=$inputdai;
    }
}
//用于收回 使用别店现金企业别店的账套
function assemLiushuiOtherXHQYRe(&$other_data,$amount,$inputjie,$inputdai,$date,$cur_name,$cur_code,$car_no,$ower_shop_name,$other_shop_code,$ower_jie_code){   
    if($amount>0){
        $date=date('m-d',strtotime($date));
        $inputjie["FAccountID"]="2131"; 
        $inputjie["FExplanation"]=$date." ".$ower_shop_name."冲减".$cur_name." ".$car_no." 冲减预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]=$ower_jie_code;//使用门店的 往来代码
        $inputdai["FExplanation"]=$date." ".$ower_shop_name."冲减".$cur_name." ".$car_no." 冲减预收款";
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("001"=>$cur_code,"012"=>$other_shop_code);
        $datedai=array("012"=>$other_shop_code);//预存门店的 门店代码
        
        $inputjie["data"]=$datejie;
        $other_data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $other_data[]=$inputdai;
    }
}

function getlase(&$shop_array,$urr_amount,$_mdb,$inputjie,$inputdai,$cur_type,$user_type,$cur_name,$car_no,$ower_msk,$cur_code,$date){//$urr_amount 要求结果集结构一致
    foreach ($urr_amount as $_ua) {           
        if($_ua->shop_id==$_mdb->id){//说明本门店使用
            if($_ua->acount_type==2){//如果是使用的保险预存
                assemLiushuiBX($shop_array[$_mdb->id]["bx"],$_ua->amount,$inputjie,$inputdai,$cur_type,$car_no,$_mdb->kingdee_shopnum,$_mdb->kingdee_bxcode);
            }else{//如果使用现金
                if ($user_type==1) {//个人 
                    assemLiushuiXHGR($shop_array[$_mdb->id]["xj"],$_ua->amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$_mdb->kingdee_shopnum);
                }else{
                    assemLiushuiXHQY($shop_array[$_mdb->id]["xj"],$_ua->amount,$inputjie,$inputdai,$date,$cur_name,$cur_code,$_mdb->kingdee_shopnum);
                }
            }  
        }else{//说明跨门店使用
            $usr_msk=MaintenanceShopKingdee::where("shop_id",$_ua->shop_id)->where("code","1133.01")->first();//本门店的现金账套
            if(is_null($usr_msk)){
                Log::error("门店 ".$_ua->shop_id."内部往来科目异常");
                return false;
            }
            if($_ua->acount_type==2){//如果是使用的保险预存   
                assemLiushuiOWerBX($shop_array[$_mdb->id]["bx"],$_ua->amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$_ua->name,$_mdb->kingdee_shopnum,$usr_msk->acount);
                assemLiushuiOtherBX($shop_array[$_ua->shop_id]["bx"],$_ua->amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$_mdb->name,$_ua->kingdee_shopnum,$ower_msk->acount,$_ua->kingdee_bxcode);
            }else{
                if($user_type==1) {//个人 
                    assemLiushuiOWerXHGR($shop_array[$_mdb->id]["xj"],$_ua->amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$_ua->name,$_mdb->kingdee_shopnum,$usr_msk->acount);
                    assemLiushuiOtherXHGR($shop_array[$_ua->shop_id]["xj"],$_ua->amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$_mdb->name,$_ua->kingdee_shopnum,$ower_msk->acount);
                }else{
                    assemLiushuiOWerXHQY($shop_array[$_mdb->id]["xj"],$_ua->amount,$inputjie,$inputdai,$date,$cur_name,$car_no,$_ua->name,$_mdb->kingdee_shopnum,$usr_msk->acount);
                    assemLiushuiOtherXHQY($shop_array[$_ua->shop_id]["xj"],$_ua->amount,$inputjie,$inputdai,$date,$cur_name,$cur_code,$car_no,$_mdb->name,$_ua->kingdee_shopnum,$ower_msk->acount);
                }
            }
        }
    }
    return true;
}

function getlaseRec(&$shop_array,$urr_amount,$_mdb,$inputjie,$inputdai,$cur_type,$user_type,$cur_name,$car_no,$ower_msk,$cur_code,$date){//$urr_amount 要求结果集结构一致
    foreach ($urr_amount as $_ua) {     
        if($_ua->shop_id==$_mdb->id){//说明本门店使用
            if($_ua->acount_type==2){//如果是使用的保险预存
                assemLiushuiBXRe($shop_array[$_mdb->id]["bx"],$_ua->amount,$inputjie,$inputdai,$cur_type,$cur_code,$_mdb->kingdee_shopnum,$_mdb->kingdee_bxcode,$cur_name);
            }else{//如果使用现金              
                assemLiushuiXHQYRe($shop_array[$_mdb->id]["xj"],$_ua->amount,$inputjie,$inputdai,$date,$cur_name,$cur_code,$_mdb->kingdee_shopnum);
            }  
        }else{//说明跨门店使用
            $usr_msk=MaintenanceShopKingdee::where("shop_id",$_ua->shop_id)->where("code","1133.01")->first();//本门店的现金账套
            if(is_null($usr_msk)){
                Log::error("门店 ".$_ua->shop_id."内部往来科目异常");
                return false;
            }
            if($_ua->acount_type==2){//如果是使用的保险预存 
                assemLiushuiOWerBXRe($shop_array[$_mdb->id]["bx"],$_ua->amount,$inputjie,$inputdai,$date,$cur_type,$cur_code,$car_no,$_ua->name,$_mdb->kingdee_shopnum,$usr_msk->acount);
                assemLiushuiOtherBXRe($shop_array[$_ua->shop_id]["bx"],$_ua->amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$_mdb->name,$_ua->kingdee_shopnum,$ower_msk->acount,$_ua->kingdee_bxcode);
            }else{                
                assemLiushuiOWerXHQYRe($shop_array[$_mdb->id]["xj"],$_ua->amount,$inputjie,$inputdai,$date,$cur_name,$cur_code,$car_no,$_ua->name,$_mdb->kingdee_shopnum,$usr_msk->acount);
                assemLiushuiOtherXHQYRe($shop_array[$_ua->shop_id]["xj"],$_ua->amount,$inputjie,$inputdai,$date,$cur_name,$cur_code,$car_no,$_mdb->name,$_ua->kingdee_shopnum,$ower_msk->acount);
                
            }
        }
    }
    return true;
}



function getMaintain($shop_id,$data,$date){ 
    $request = array(
        'token' => 1,
        'zhangtao' => "sqlsrv_scs",
        'params' => json_encode($data),
    );

    $response = Httpful::post(Config::get('config.kingdee_local_api')."/kingdeescs/importVoucher")
        ->timeoutIn(15)
        ->body($request)
        ->sendsForm()->send();

    $response = json_decode($response->body);
    Log::info($response->msg);
    $kvs=new KingdeeVoucherStatus();
    $kvs->date=$date;
    $kvs->shop_id=$shop_id;
    $kvs->type="liushui_inout";
    if($response->code==0){//说明成功
        $kvs->status=1;
    }else{
        $kvs->status=0;
    }
    $kvs->save();
}